package com.sg.service.biz.operation.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.operation.req.QueryDataSeparationListReq;
import com.sg.dto.biz.operation.req.JudgeDataSeparationReqDto;
import com.sg.dto.biz.operation.req.QueryDataSeparationDetailReqDto;
import com.sg.dto.biz.operation.req.QueryDataSeparationListReqDto;
import com.sg.dto.biz.operation.req.UpdateDataSeparationReqDto;
import com.sg.dto.biz.operation.res.JudgeDataSeparationRespDto;
import com.sg.dto.biz.operation.res.QueryDataSeparationDetailRespDto;
import com.sg.dto.biz.operation.res.QueryDataSeparationListRespDto;
import com.sg.dto.biz.operation.res.UpdateDataSeparationRespDto;
import com.sg.dto.integration.component.ImplementTwoValuesIsEqualityComReqDto;
import com.sg.dto.integration.component.ImplementTwoValuesIsEqualityComRespDto;
import com.sg.entity.OmsPlatformOperationSeparatedData;
import com.sg.service.base.operation.MOmsPlatformOperationSeparatedDataService;
import com.wicket.okrcomponent.integration.InterfaceModeService;
import com.sg.common.exception.Assert;
import com.wicket.okroperation.common.exception.BizException;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DeploymentServiceImpl
        implements com.wicket.okroperation.biz.service.DeploymentService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MOmsPlatformOperationSeparatedDataService mOmsPlatformOperationSeparatedDataService;
    @Resource
    private InterfaceModeService interfaceModeService;

    /**
     * D6修改数据分离[6931]
     * gen by moon at 9/13/2023, 10:40:51 PM
     */
    @Trace(operationName = "D6修改数据分离")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateDataSeparationRespDto updateDataSeparation(UpdateDataSeparationReqDto reqDto) {


        //步骤0: 6-1-02修改数据分离 - updateDataSeparation
        boolean bOOLEAN;
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData = new OmsPlatformOperationSeparatedData();
        if (reqDto != null) {
            omsPlatformOperationSeparatedData.setPrdTableEncryptionName(reqDto.getPrdTableEncryptionName());//SimpleFieldAssign//sourceId:976499_1
            omsPlatformOperationSeparatedData.setIsNeeddataSeparated(reqDto.getIsNeeddataSeparated());//SimpleFieldAssign//sourceId:976121_1
            omsPlatformOperationSeparatedData.setDataSeparatedStrategyTypeCode(reqDto.getDataSeparatedStrategyTypeCode());//SimpleFieldAssign//sourceId:976122_1
        }

        /*6-1-02修改数据分离[6928]   */

        bOOLEAN = mOmsPlatformOperationSeparatedDataService.updateDataSeparation(omsPlatformOperationSeparatedData)/*vcase invoke 本地 method 方法调用;*/;


        UpdateDataSeparationRespDto retData = new UpdateDataSeparationRespDto();


        return retData;
    }

    /**
     * D6查询数据分离列表[6932]
     * gen by moon at 5/22/2023, 9:26:29 PM
     */
    @Trace(operationName = "D6查询数据分离列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryDataSeparationListRespDto queryDataSeparationList(QueryDataSeparationListReqDto reqDto) {


        List<OmsPlatformOperationSeparatedData> listOmsPlatformOperationSeparatedData_1 = new ArrayList<>();
//步骤0: 6-1-02查询数据分离列表 - queryDataSeparationList
        List<OmsPlatformOperationSeparatedData> listOmsPlatformOperationSeparatedData = new ArrayList<>();
        QueryDataSeparationListReq queryDataSeparationListReq = new QueryDataSeparationListReq();
        if (reqDto != null) {
            queryDataSeparationListReq.setTableCnName(reqDto.getTableCnName());//SimpleFieldAssign//sourceId:976126_1
            queryDataSeparationListReq.setDataSeparatedStatus(reqDto.getDataSeparatedStatus());//SimpleFieldAssign//sourceId:976127_1
        }

        /*6-1-02查询数据分离列表[6929]   */

        listOmsPlatformOperationSeparatedData = mOmsPlatformOperationSeparatedDataService.queryDataSeparationList(queryDataSeparationListReq)/*vcase invoke 本地 method 方法调用;*/;


        listOmsPlatformOperationSeparatedData_1 = listOmsPlatformOperationSeparatedData;

        QueryDataSeparationListRespDto retData = new QueryDataSeparationListRespDto();
        retData.setDataSeparationList(listOmsPlatformOperationSeparatedData_1.stream().map(item -> BeanUtil.toBean(item, DataSeparationDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:976131_1


        return retData;
    }

    /**
     * D6查询数据分离详情[6933]
     * gen by moon at 9/13/2023, 10:40:57 PM
     */
    @Trace(operationName = "D6查询数据分离详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryDataSeparationDetailRespDto queryDataSeparationDetail(QueryDataSeparationDetailReqDto reqDto) {


        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData_2 = null;
//步骤0: 6-1-02查询数据分离详情 - queryDataSeparationDetail
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData = null;
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData_1 = new OmsPlatformOperationSeparatedData();
        if (reqDto != null) {
            omsPlatformOperationSeparatedData_1.setOperationSeparatedDataId(reqDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976132_1
        }

        /*6-1-02查询数据分离详情[6930]   */
        Assert.isNull(omsPlatformOperationSeparatedData_1.getOperationSeparatedDataId(), "D6查询数据分离详情-6-1-02查询数据分离详情-分离数据表主键ID不能为空", false);
        omsPlatformOperationSeparatedData = mOmsPlatformOperationSeparatedDataService.queryDataSeparationDetail(omsPlatformOperationSeparatedData_1)/*vcase invoke 本地 method 方法调用;*/;


        omsPlatformOperationSeparatedData_2 = omsPlatformOperationSeparatedData;

        QueryDataSeparationDetailRespDto retData = new QueryDataSeparationDetailRespDto();
        if (omsPlatformOperationSeparatedData_2 != null) {
            retData.setOperationSeparatedDataId(omsPlatformOperationSeparatedData_2.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976147_1
            retData.setTableId(omsPlatformOperationSeparatedData_2.getTableId());//SimpleFieldAssign//sourceId:976148_1
            retData.setTableCnName(omsPlatformOperationSeparatedData_2.getTableCnName());//SimpleFieldAssign//sourceId:976149_1
            retData.setTableEnName(omsPlatformOperationSeparatedData_2.getTableEnName());//SimpleFieldAssign//sourceId:976150_1
            retData.setToolTableEncryptionName(omsPlatformOperationSeparatedData_2.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:976151_1
            retData.setPrdTableEncryptionName(omsPlatformOperationSeparatedData_2.getPrdTableEncryptionName());//SimpleFieldAssign//sourceId:976152_1
            retData.setEncryptionNameIsSame(omsPlatformOperationSeparatedData_2.getEncryptionNameIsSame());//SimpleFieldAssign//sourceId:976153_1
            retData.setBelongSpaceId(omsPlatformOperationSeparatedData_2.getBelongSpaceId());//SimpleFieldAssign//sourceId:976154_1
            retData.setIsNeeddataSeparated(omsPlatformOperationSeparatedData_2.getIsNeeddataSeparated());//SimpleFieldAssign//sourceId:976155_1
            retData.setDataSeparatedStrategyTypeCode(omsPlatformOperationSeparatedData_2.getDataSeparatedStrategyTypeCode());//SimpleFieldAssign//sourceId:976156_1
            retData.setDataSeparatedStatus(omsPlatformOperationSeparatedData_2.getDataSeparatedStatus());//SimpleFieldAssign//sourceId:976157_1
            retData.setOperationInductionId(omsPlatformOperationSeparatedData_2.getOperationInductionId());//SimpleFieldAssign//sourceId:976158_1
            retData.setOperateTime(omsPlatformOperationSeparatedData_2.getOperateTime());//SimpleFieldAssign//sourceId:976159_1
        }


        return retData;
    }

    /**
     * D6判断数据分离[6937]
     * gen by moon at 5/23/2023, 5:02:45 AM
     */
    @Trace(operationName = "D6判断数据分离")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JudgeDataSeparationRespDto judgeDataSeparation(JudgeDataSeparationReqDto reqDto) {


        //virtualUsage 6-1-02查询数据分离详情  42720
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData = null;
        OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData_1 = new OmsPlatformOperationSeparatedData();
        if (reqDto != null) {
            omsPlatformOperationSeparatedData_1.setOperationSeparatedDataId(reqDto.getOperationSeparatedDataId());//SimpleFieldAssign//sourceId:976217_1_42720
        }

        /*6-1-02查询数据分离详情[6930]   */
        Assert.isNull(omsPlatformOperationSeparatedData_1.getOperationSeparatedDataId(), "D6判断数据分离-6-1-02查询数据分离详情-分离数据表主键ID不能为空", false);
        omsPlatformOperationSeparatedData = mOmsPlatformOperationSeparatedDataService.queryDataSeparationDetail(omsPlatformOperationSeparatedData_1)/*vcase invoke 本地 method 方法调用;*/;


//virtualUsage D2执行两个值比较是否相等(公共)  42721
        ImplementTwoValuesIsEqualityComRespDto implementTwoValuesIsEqualityComRespDto = null;
        if (omsPlatformOperationSeparatedData != null) {
            ImplementTwoValuesIsEqualityComReqDto implementTwoValuesIsEqualityComReqDto = new ImplementTwoValuesIsEqualityComReqDto();
            if (omsPlatformOperationSeparatedData != null) {
                implementTwoValuesIsEqualityComReqDto.setCustomField(omsPlatformOperationSeparatedData.getToolTableEncryptionName());//SimpleFieldAssign//sourceId:976231_1_42721
                implementTwoValuesIsEqualityComReqDto.setCustomField1(omsPlatformOperationSeparatedData.getPrdTableEncryptionName());//SimpleFieldAssign//sourceId:976232_1_42721
            }

            /*D2执行两个值比较是否相等(公共)[6425]   */
            Assert.isNull(implementTwoValuesIsEqualityComReqDto.getCustomField(), "D6判断数据分离-D2执行两个值比较是否相等(公共)-自定义字段不能为空", false);
            Assert.isNull(implementTwoValuesIsEqualityComReqDto.getCustomField1(), "D6判断数据分离-D2执行两个值比较是否相等(公共)-自定义字段1不能为空", false);
            implementTwoValuesIsEqualityComRespDto = interfaceModeService.implementTwoValuesIsEqualityCom(implementTwoValuesIsEqualityComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        }
        if ((implementTwoValuesIsEqualityComRespDto != null && implementTwoValuesIsEqualityComRespDto.getTureOrFalse() != null && implementTwoValuesIsEqualityComRespDto.getTureOrFalse().equals("FALSE"))) {
            //if(D2执行两个值比较是否相等(公共).是否相等 等于 否)  42722

//异常结束 42724
            throw new BizException("O10100235", "线上加密表名与设计工具不一致！", false);
        } else if ((implementTwoValuesIsEqualityComRespDto != null && implementTwoValuesIsEqualityComRespDto.getTureOrFalse() != null && implementTwoValuesIsEqualityComRespDto.getTureOrFalse().equals("TRUE"))) {
            //elseif(D2执行两个值比较是否相等(公共).是否相等 等于 是)  42725

            boolean bOOLEAN;
            OmsPlatformOperationSeparatedData omsPlatformOperationSeparatedData_3 = new OmsPlatformOperationSeparatedData();
            omsPlatformOperationSeparatedData_3.setEncryptionNameIsSame("TRUE");//sourceId:976497_1_42727

            /*6-1-02修改数据分离[6928]   */
            Assert.isNull(omsPlatformOperationSeparatedData_3.getEncryptionNameIsSame(), "D6判断数据分离-6-1-02修改数据分离-加密表名是否一致不能为空", false);
            bOOLEAN = mOmsPlatformOperationSeparatedDataService.updateDataSeparation(omsPlatformOperationSeparatedData_3)/*vcase invoke 本地 method 方法调用;*/;


        }
        JudgeDataSeparationRespDto retData = new JudgeDataSeparationRespDto();


        return retData;
    }
    //
}
